%{
  #include <stdio.h>
  #include "eval-upmc.h"

  extern Expression* result_expr;
  extern int parse_error_flag;
  extern char* error_message;

  extern Expression* new_integer(int i);
  extern Expression* new_double(double b);
  extern Expression* new_binary_operation(char symbol, Expression* left, Expression* right);
  extern int readInputForLexer(char* buffer,int *numBytesRead,int maxBytesToRead);

%}

%union { int int_value; double double_value; Expression* expr_value; }

%token <int_value>INT
%token <double_value>DOUBLE


%type <expr_value>expr 

%left '-' '+'
%left '*' '/'
%left '(' ')'

%start COMPLET_EXPR

%%

COMPLET_EXPR : 
'$' '[' expr ']' { result_expr = $3; return; }
;

expr :  expr '+' expr        { $$ = new_binary_operation('+', $1, $3); }
| expr '-' expr              { $$ = new_binary_operation('-', $1, $3); }
| expr '*' expr              { $$ = new_binary_operation('*', $1, $3); }
| expr '/' expr              { $$ = new_binary_operation('/', $1, $3); }
| '(' expr ')'               { $$ = $2; }
| INT                        { $$ = new_integer((int)$1); }
| DOUBLE                     { $$ = new_double((double)$1); }
      ;
%%

int yyerror(char *s) {
  parse_error_flag = 1; 
  error_message= "Syntax error.";
  return 0;
}
